package com.losdun.product.service.impl;

import com.losdun.common.utils.DateUtils;
import com.losdun.common.utils.StringUtils;
import com.losdun.product.domain.Category;
import com.losdun.product.domain.vo.TreeSelectVo;
import com.losdun.product.mapper.CategoryMapper;
import com.losdun.product.service.ICategoryService;
import com.losdun.product.service.IProductService;
import com.losdun.product.utils.TreeSelectBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 产品分类Service业务层处理
 * 
 * @author sword.ye
 * @date 2025-02-27
 */
@Service
public class CategoryServiceImpl implements ICategoryService 
{
    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private IProductService productService;

    /**
     * 查询产品分类
     * 
     * @param id 产品分类主键
     * @return 产品分类
     */
    @Override
    public Category selectCategoryById(Long id)
    {
        return categoryMapper.selectCategoryById(id);
    }

    /**
     * 查询产品分类列表
     * 
     * @param category 产品分类
     * @return 产品分类
     */
    @Override
    public List<Category> selectCategoryList(Category category)
    {
        return categoryMapper.selectCategoryList(category);
    }

    /**
     * 新增产品分类
     * 
     * @param category 产品分类
     * @return 结果
     */
    @Override
    public int insertCategory(Category category)
    {
        category.setCreateTime(DateUtils.getNowDate());
        return categoryMapper.insertCategory(category);
    }

    /**
     * 修改产品分类
     * 
     * @param category 产品分类
     * @return 结果
     */
    @Override
    public int updateCategory(Category category)
    {
        category.setUpdateTime(DateUtils.getNowDate());
        return categoryMapper.updateCategory(category);
    }

    /**
     * 批量删除产品分类
     * 
     * @param ids 需要删除的产品分类主键
     * @return 结果
     */
    @Override
    public int deleteCategoryByIds(Long[] ids)
    {
        return categoryMapper.deleteCategoryByIds(ids);
    }

    /**
     * 删除产品分类信息
     * 
     * @param id 产品分类主键
     * @return 结果
     */
    @Override
    public int deleteCategoryById(Long id)
    {
        return categoryMapper.deleteCategoryById(id);
    }

    @Override
    public List<TreeSelectVo> selectCategoryTree(Category category) {
        List<Category> categoryList = categoryMapper.selectCategoryList(category);
        //List<Category> categoryTrees= this.buildCategoryTree(categoryList);
        //return categoryTrees.stream().map(TreeSelectVo::new).collect(Collectors.toList());
        return new TreeSelectBuilder<Category>().buildTree(categoryList);
    }

    @Override
    public BigDecimal getCategoryMinPrice(Long cateId) {
        return productService.getProductMinPrice(cateId);
    }

    /**
     * 构建前端所需要树结构
     *
     * @param
     * @return 树结构列表
     */
    private List<Category> buildCategoryTree(List<Category> categoryList)
    {
        List<Category> returnList = new ArrayList<Category>();
        List<Long> tempList = categoryList.stream().map(Category::getId).collect(Collectors.toList());
        for (Category category : categoryList)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(category.getParentId()))
            {
                recursionFn(categoryList, category);
                returnList.add(category);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = categoryList;
        }
        return returnList;
    }
    /**
     * 递归列表
     */
    private void recursionFn(List<Category> list, Category t)
    {
        // 得到子节点列表
        List<Category> childList = getChildList(list, t);
        t.setChildren(childList);
        for (Category tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<Category> getChildList(List<Category> list, Category t)
    {
        List<Category> tlist = new ArrayList<Category>();
        Iterator<Category> it = list.iterator();
        while (it.hasNext())
        {
            Category n = (Category) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }
    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<Category> list, Category t)
    {
        return getChildList(list, t).size() > 0;
    }

}
